package system_sms_service

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *Sms) Cancel(ctx context.Context, log *model.SystemSmsLog) errors.Error {
	if nil == log {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_MS.S_MS.C_EL.114941 log is nil")

		return errors.ErrorServerInternalError("ISS_MS.S_MS.C_EL.144952", "异常的传入参数")
	}

	if *log.Status != model.SystemSmsLogStatusPending {
		return errors.New(errors.ErrorTypeInvalidRequest, "ISS_MS.S_MS.C_EL.195012", "非待发送状态无法取消")
	}

	log.Status = pointer.New(model.SystemSmsLogStatusCanceled)

	if err := cls.smsLog.SaveSystemSmsLog(ctx, log); nil != err {
		return err.Relation(errors.ErrorServerInternalError("ISS_MS.S_MS.C_EL.255016", "取消失败"))
	}

	return nil
}
